package com.lxt.cloud.rest;

import com.lxt.common.mvc.msg.PageEntity;
import com.lxt.cloud.service.GeneratorService;
import com.lxt.cloud.utils.GeneratorUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ace on 2017/8/25.
 */
@Controller
@RequestMapping("/base/generator")
public class GeneratorRest {
private ObjectMapper op=new ObjectMapper();
    @Autowired
    private GeneratorService generatorService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/page")
    public PageEntity<Map<String, Object>> list(@RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = generatorService.queryList(params);
        int total = generatorService.queryTotal(params);
        PageEntity<Map<String, Object>> ret = new PageEntity<Map<String, Object>>();//(total, result);
        ret.setListData(result);
        ret.setTotal(total);
        return ret;
    }

    /**
     * 生成代码
     */
    @RequestMapping("/code")
    public void code(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String[] tableNames = new String[]{};
        String tables = request.getParameter("tables");
//        tableNames = JSON.parseArray(tables).toArray(tableNames);
        ArrayList<String> tableNameList= op.readValue(tables, ArrayList.class);
        Object[] tableNames = tableNameList.toArray();
        String moduleName = request.getParameter("moduleName");
        String packageName = request.getParameter("packageName");
        boolean autoModule = Boolean.parseBoolean(request.getParameter("autoModule"));
        String fileTemplate = request.getParameter("fileTemplate");
        if (StringUtils.isEmpty(packageName)) {
            packageName = "com.lxt";
        }
        if (StringUtils.isEmpty(moduleName)) {
            if (!autoModule || tableNames.length == 1) {
                String tablePrefix = GeneratorUtils.config.getString("tablePrefix");
                String tableName=(String)tableNames[0];
                if (tableName.startsWith(tablePrefix)) {
                    moduleName = tableName.substring(tableName.indexOf(tablePrefix) + tablePrefix.length());
                } else {
                    moduleName = tableName;
                }
                autoModule = false;
            }
        }
        byte[] data = generatorService.generatorCode(tableNames, packageName, moduleName, autoModule, fileTemplate);

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + packageName + (StringUtils.isEmpty(moduleName) ? "" : ("." + moduleName)) + ".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
}
